#!/usr/bin/tclsh

set data [lindex $argv 0]

set cmds {primes0 primes1 primes2 primes3 \
              seq1 seq2 seq3 par1 par2 par3}

set fd [open $data "r"]

while {[gets $fd line] >= 0} {
    set fields [split $line ","]
    set prog [lindex $fields 0]
    set arg [lindex $fields 1]
    set time [lindex $fields 2]

    if {[info exists total-${prog}($arg)]} {
        set val [set total-${prog}($arg)]
    } else {
        set val 0.0
    }
    set total-${prog}($arg) [expr {$val + $time}]
    incr count-${prog}($arg)
}

close $fd

proc put-data {fd prog} {
    global total-$prog count-$prog

    foreach t [lsort -integer [array names count-${prog}]] {
        set total [set total-${prog}($t)]
        set count [set count-${prog}($t)]
        set mean [expr {($total+0.0) / $count}]
        puts $fd "$t,$mean"
    }
    puts $fd "e"
}    

# Write gnuplot scripts
set fd [open "plot1" w]
puts $fd {set logscale xy
set datafile separator ","
set term eps
set output "plot1.eps"
plot "-" using 1:2 title "primes0" with linespoints, \
     "-" using 1:2 title "primes1" with linespoints, \
     "-" using 1:2 title "primes2" with linespoints, \
"-" using 1:2 title "primes3" with linespoints}
put-data $fd primes0
put-data $fd primes1
put-data $fd primes2
put-data $fd primes3
close $fd
exec gnuplot plot1 >@stdout 2>@stderr

set fd [open "plot2" w]
puts $fd {set logscale xy
set datafile separator ","
set term eps
set output "plot2.eps"
plot "-" using 1:2 title "seq1" with linespoints, \
     "-" using 1:2 title "seq2" with linespoints, \
     "-" using 1:2 title "seq3" with linespoints, \
     "-" using 1:2 title "par1" with linespoints, \
     "-" using 1:2 title "par2" with linespoints, \
     "-" using 1:2 title "par3" with linespoints}
put-data $fd seq1
put-data $fd seq2
put-data $fd seq3
put-data $fd par1
put-data $fd par2
put-data $fd par3
close $fd
exec gnuplot plot2 >@stdout 2>@stderr
